package com.cscec.omega.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cscec.omega.entity.ProjectEntity;
import com.cscec.omega.service.ProjectService;
import com.cscec.omega.utils.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @GetMapping("/find")
    public ResponseEntity findAll(@RequestBody JSONObject jsonObject){
        ProjectEntity entity = JSON.toJavaObject(jsonObject, ProjectEntity.class);
        List<ProjectEntity> list = projectService.findAll(entity);
        return new ResponseEntity(20000, "查询成功",  list);
    }

    @PostMapping("/add")
    public ResponseEntity add(@RequestBody JSONObject jsonObject){
        ProjectEntity entity = JSON.toJavaObject(jsonObject, ProjectEntity.class);
        ProjectEntity projectEntity = projectService.save(entity);
        return new ResponseEntity(20000, "操作成功",  projectEntity);
    }

    @GetMapping("/{id}")
    public ResponseEntity getObject(@PathVariable(value = "id") Integer id){
        ProjectEntity entity = projectService.getProject(id);
        return new ResponseEntity(20000, "", entity);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity delete(@PathVariable(value = "id") Integer id) {
        projectService.delete(id);
        return new ResponseEntity(20000, "删除成功");
    }
}
